package com.moonciki.artadmin.web.service.game.impl;

import com.moonciki.artadmin.core.base.BaseServiceImpl;
import com.moonciki.artadmin.core.enums.FileTypeEnum;
import com.moonciki.artadmin.core.exception.CustomException;
import com.moonciki.artadmin.core.model.FileWrapper;
import com.moonciki.artadmin.core.model.PageData;
import com.moonciki.artadmin.core.model.PageParameter;
import com.moonciki.artadmin.core.tools.CustomPageHelper;
import com.moonciki.artadmin.core.utils.excel.ExcelUtils;
import com.moonciki.artadmin.core.utils.excel.ExcelWriter;

import com.moonciki.artadmin.web.mybatis.entity.fuzhu.FzGame;
import com.moonciki.artadmin.web.mybatis.mapper.fuzhu.FzGameMapper;
import com.moonciki.artadmin.web.service.game.IFzGameService;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 游戏列Service业务层处理
 * @author ruoyi
 * @date 2022-02-16
 */
@Service
public class FzGameServiceImpl extends BaseServiceImpl implements IFzGameService {

    @Autowired
    private FzGameMapper fzGameMapper;


    @Override
    public FzGame getById(Long id) {
        FzGame fzGame = fzGameMapper.selectByPrimaryKey(id);
        return fzGame;
    }

    @Override
    public PageData<FzGame> getPageList(PageParameter pageParameter, FzGame fzGame) {
        CustomPageHelper<FzGame> sqlPager = pageParameter.startPagger();
        List<FzGame> select = fzGameMapper.select(fzGame);
        PageData<FzGame> pageData = sqlPager.endPagger(select);
        return pageData;
    }

    @Override
    public List<FzGame> getList(FzGame fzGame) {
        List<FzGame> select = fzGameMapper.select(fzGame);
        return select;
    }

    @Override
    public FileWrapper export(FzGame fzGame) {
        FileWrapper fileWrapper = new FileWrapper();

        List<FzGame> list = this.getList(fzGame);

        String sheetName = "游戏列数据";
        String fileName = sheetName + FileTypeEnum.xlsx.getSuffix();

        String[] titleArray = {
            "$column.columnComment", "游戏名称", "游戏编码", "启用禁用（0禁用；1启用）"
        };
        String[] fieldArray = {
            "id", "name", "code", "enable"
        };

        ExcelWriter excelWriter = null;

        try {
            excelWriter = ExcelWriter.getExcelWriter(fileName, 0, sheetName);
            excelWriter.writeTitleData(titleArray);

            if(CollectionUtils.isNotEmpty(list)){
                for(FzGame oneVo: list){
                    Object[] rowData = ExcelUtils.objToExcelArray(oneVo, fieldArray);

                    excelWriter.writeData(rowData);
                }
            }

            byte[] fileBytes = excelWriter.getBytes();

            fileWrapper.setDataBytes(fileBytes);
            fileWrapper.setFileName(fileName);
        } catch (Exception e) {
            throw CustomException.createException(e);
        } finally {
            if (excelWriter != null) {
                try {
                    excelWriter.close();
                } catch (Exception e) {
                    throw CustomException.createException(e);
                }
            }
        }
        return fileWrapper;
    }

    @Override
    public FzGame insertFzGame(FzGame fzGame) {

        int i = fzGameMapper.insertSelective(fzGame);
        return fzGame;
    }

    @Override
    public int updateFzGame(FzGame fzGame) {
        Long id = fzGame.getId();

        return fzGameMapper.updateByPrimaryKeySelective(fzGame);
    }

    @Override
    public int deleteByIds(List<Long> idList) {

        Example exa = new Example(FzGame.class);
        exa.and().andIn("id", idList);
        int i = fzGameMapper.deleteByExample(exa);
        return i;
    }

    @Override
    public int deleteById(Long id) {
        int i = fzGameMapper.deleteByPrimaryKey(id);
        return i;
    }

}
